package com.itheima.reflect.myreflect3;

import java.lang.reflect.Constructor;

public class ReflectDemo1 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException {
        //method1();
        //method2();
        //method3();
        //method4();

    }

    private static void method4() throws ClassNotFoundException, NoSuchMethodException {
        Class<?> clazz = Class.forName("com.itheima.reflect.myreflect3.Student");
        Constructor<?> constructor = clazz.getDeclaredConstructor(String.class);
        System.out.println(constructor);
    }

    private static void method3() throws ClassNotFoundException, NoSuchMethodException {
        Class<?> clazz = Class.forName("com.itheima.reflect.myreflect3.Student");
        // 小括号中一定要跟构造方法的形参保持一致
        Constructor<?> constructor1 = clazz.getConstructor();
        System.out.println(constructor1);
        Constructor<?> constructor2 = clazz.getConstructor(String.class, int.class);
        System.out.println(constructor2);
        // 因为 Student 类中没有只有一个int的构造方法,所以这里会报异常
        Constructor<?> constructor3 = clazz.getConstructor(int.class);
        System.out.println(constructor3);
    }

    private static void method2() throws ClassNotFoundException {
        Class<?> clazz = Class.forName("com.itheima.reflect.myreflect3.Student");
        // 返回所有构造方法
        Constructor<?>[] constructors = clazz.getDeclaredConstructors();
        for (Constructor<?> constructor : constructors) {
            System.out.println(constructor);
        }
    }

    private static void method1() throws ClassNotFoundException {
        Class<?> clazz = Class.forName("com.itheima.reflect.myreflect3.Student");
        // 返回所有公共构造方法对象的数组
        Constructor<?>[] constructors = clazz.getConstructors();
        for (Constructor<?> constructor : constructors) {
            System.out.println(constructor);
        }
    }
}
